/*
 * pcb_queue.c
 *
 *  Created on: Jun 6, 2013
 *      Author: Aden
 */

/*
 * queue.c
 *
 *  Created on: May 23, 2013
 *      Author: Aden
 */


#include <stdio.h>
#include <stdlib.h>
#include "global.h"
#include "pcb_queue.h"


/*
 * Queue Constructor
 */
PCBQueuePtr newPcbQueueConstructor(void)
{
	PCBQueuePtr queue  = (PCBQueuePtr) malloc(sizeof(pcbStr));
	int i;
	for(i = 0 ; i < 1000; i++){
    queue->pcb_ready_queue[i] = (PCBPtr) malloc(sizeof(PCBStr));
	}
	queue->size = 0;
	queue->back = -1;
	queue->front = 0;



	//pointers to functions
	queue->addToQ= addToReadyQ;
	queue->dequeueFromQ = dequeueFromReadyQ;
	queue->empty = emptyR;
  
  queue->display = displayr;

	return queue;
}
/**
 * Insert a value at the end of the queue
 * this -
 * value - the value
 */
int addToReadyQ(PCBQueuePtr this, PCBPtr process)
{
	if(this->pcb_ready_queue == NULL)
	{
		return NULL_POINTER_ERROR;
	}
	else if(this->back == 1000)
	{
		printw("\nOVERFLOW!!!");
	}
	else
	{

		this->back =(this->back + 1) % MAX_QUEUE_SIZE;
		this->pcb_ready_queue[this->back] =  process;
		this->size++;
	}

	return NO_ERROR;
}
/*
 * Return the value in front of the queue
 * this -
 */
PCBPtr dequeueFromReadyQ(PCBQueuePtr this)
{
	void * value = this->pcb_ready_queue[this->front];
	if(this->size == 0)
	{
			}
	else
	{
		this->front=(this->front + 1) % MAX_QUEUE_SIZE;
		this->size--;
	}
	return value;
}
/**
 * Return empty if the queue is or otherwise
 * this -
 *
 */
int emptyR(PCBQueuePtr this)
{
	return this->size == 0;
}

/*
 * Queue object representation
 * this - the QueuePtr
 */
int displayr(PCBQueuePtr this)
{
  PCBPtr* queue = this->pcb_ready_queue;
	int i;
	printw("Qr: %d[ ", this->size);
	for(i = this->front;  i <= this->back; i++)
	{
		printw("%d ",queue[i]->pid);
	}
	printw("]\n");
	return NO_ERROR;
}

//int main(void)
//{
//
//
//
//	return 0;
//}

